local extension = Package:new("tea_ex_c")
extension.extensionName = "wild_collection"
Fk:loadTranslationTable{
  ["tea_ex_c"] = "茶水闲设三",
}
local U = require "packages/utility/utility"
local Est = require "packages/wild_collection/utility"

local weizhen = General(extension, "tea__weizhen", "wei", 3)
local liaoge = fk.CreateTriggerSkill{
  name = "tea_liaoge",
  anim_type = "special",
  events = {fk.TurnStart, fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    if event == fk.TurnStart then
      return player:hasSkill(self) and not player:isNude() and target ~= player and #player:getPile("liaoge_pile") == 0
    else
      return player:hasSkill(self) and #player:getPile("liaoge_pile") > 0
    end
  end,
  on_cost = function(self, event, target, player, data)
    if event == fk.TurnStart then
      local cid = player.room:askForCard(player, 1, 1, true, self.name, true, ".", "#tea_liaoge-invoke")
      if #cid > 0 then
        self.cost_data = cid
        return true
      end
    else
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.TurnStart then
      local cid = self.cost_data
      player:addToPile("liaoge_pile", cid, false, skillName, player.id)
    else
      local cids = player:getPile("liaoge_pile")
      local c = Fk:getCardById(cids[1])
      local usedSalsh
      room.logic:getEventsOfScope(GameEvent.UseCard, 1, function (e)
        local use = e.data[1]
        if use.card.trueName == "slash" then
          usedSalsh = true
        end
        return false
      end, Player.HistoryTurn)
      if (usedSalsh and c.color == Card.Black) or (not usedSalsh and c.color == Card.Red) then
        room:obtainCard(player, cids, true, fk.ReasonJustMove)
        if not player.dead then
          player:drawCards(1, self.name)
        end
      else
        room:throwCard(cids, self.name, player)
      end
    end
  end,
}
local getDiscardNum = function(player)
  return #player:getCardIds("h") - player.maxHp
end
local guijian = fk.CreateViewAsSkill{
  name = "tea_guijian",
  anim_type = "special",
  pattern = ".|.|.|.|.|basic",
  interaction = function()
    local all_names = U.getAllCardNames("b")
    local names = U.getViewAsCardNames(Self, "tea_guijian", all_names)
    if #names > 0 then
      return U.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, to_select, selected)
    return table.contains(Self:getCardIds("h"), to_select) and getDiscardNum(Self) > #selected
  end,
  view_as = function(self, cards)
    if not self.interaction.data then return end
    if getDiscardNum(Self) <= 0 then
      if #cards > 0 then return end
    else
      if getDiscardNum(Self) ~= #cards then return end
    end
    local card = Fk:cloneCard(self.interaction.data)
    card.skillName = self.name
    self.cost_data = cards
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local cids = self.cost_data
    if #(cids or {}) > 0 then
      room:throwCard(cids, self.name, player, player)
    end
    if getDiscardNum(player) < 0 then
      room:setPlayerMark(player, "@@tea_guijian_debuff-turn", 1)
      player:drawCards(math.abs(getDiscardNum(player)), self.name)
    end
  end,
  enabled_at_play = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  enabled_at_response = function(self, player, response)
    return player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and not response
  end,
}
local guijian_delay = fk.CreateTriggerSkill{
  name = "#tea_guijian_delay",
  anim_type = "negative",
  events = {fk.CardUsing},
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and not player:isNude() and player:getMark("@@tea_guijian_debuff-turn") > 0 and not table.contains(data.card.skillNames, guijian.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    player.room:askForDiscard(player, 1, 1, true, self.name, false)
  end,
}
guijian:addRelatedSkill(guijian_delay)
weizhen:addSkill(liaoge)
weizhen:addSkill(guijian)
Fk:loadTranslationTable{
  ["tea__weizhen"] = "卫臻",
  ["#tea__weizhen"] = "清鉴有节",
  ["designer:tea__weizhen"] = "布林",
  ["cv:tea__weizhen"] = "茶社",
  ["illustrator:tea__weizhen"] = "啪啪三国",
  ["tea_liaoge"] = "料戈",
  [":tea_liaoge"] = "其他角色回合开始时，你可将一张牌扣置于武将牌上，并于回合结束时展示之。若此牌为黑色/红色且本回合有/没有角色使用过【杀】，"..
  "你获得此牌并摸一张牌，否则你弃置之。",
  ["liaoge_pile"] = "料戈",
  ["#tea_liaoge-invoke"] = "料戈：你可以将一张牌扣置于武将牌上，然后根据本回合是否有角色使用【杀】来执行效果",
  ["tea_guijian"] = "规鉴",
  ["#tea_guijian_delay"] = "规鉴",
  [":tea_guijian"] = "每回合限一次，你可以将手牌调整至体力上限，视为使用一张基本牌。若以此法获得牌，本回合你使用牌时弃一张牌。",
  ["@@tea_guijian_debuff-turn"] = "规鉴",
}

local mohuba = General(extension, "tea__mohuba", "wei", 4)
local lianhe = fk.CreateActiveSkill{
  name = "tea_lianhe",
  anim_type = "special",
  target_num = 1,
  target_filter = function(self, to_select, selected)
    if #selected == 0 then
      local players = Fk:currentRoom().alive_players
      players = table.filter(players, function(p)
        return (table.every(players, function(p1)
          return p.hp >= p1.hp
        end) or table.every(players, function(p2)
          return #p:getCardIds("h") >= #p2:getCardIds("h")
        end)) and not p:isKongcheng()
      end)
      if #players > 0 then
        if #Self:getTableMark("tea_lianhe_choose") == 0 then
          return table.contains(table.map(players, Util.IdMapper), to_select)
        else
          local mark = Self:getTableMark("tea_lianhe_choose")[1]
          mark = Fk:currentRoom():getPlayerById(mark)
          if mark.dead then
            return table.contains(table.map(players, Util.IdMapper), to_select)
          end
          local to = Fk:currentRoom():getPlayerById(to_select)
          if mark.dead then return false end
          return table.contains(table.map(players, Util.IdMapper), to_select) and (mark:getNextAlive() == to or mark:getLastAlive() == to)
        end
      end
    end
  end,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return table.find(Fk:currentRoom().alive_players, function(p)
      return U.canUseCardTo(Fk:currentRoom(), player, p, Fk:cloneCard("duel")) and not table.contains(player:getTableMark("tea_lianhe_record-turn"), p.id)
    end)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
   
    local expand_pile = {}
    if player ~= target then
      local names = {}
      for i = 1, #target:getCardIds("h") do
        table.insert(names, {"card_back"})
      end
      local mark = U.prepareDeriveCards(room, names, "card_back_tag")
      for i, id in ipairs(target:getCardIds("h")) do
        if player:cardVisible(id) then
          mark[i] = id
        end
      end
      room:setTag("card_back_tag", "empty")
      expand_pile = mark
    end

    --[[
    for _, id in ipairs(expand_pile) do
      room:setCardMark(Fk:getCardById(id), "tea_lianhe_visable-phase", 1)
    end]]
    --FIXME:无法暗置，等待修复ing

    local suc, dat = room:askForUseActiveSkill(player, "tea_lianhe_viewas", "#tea_lianhe-invoke", true, {
      is_self = player == target,
      expand_pile = expand_pile,
    })
    for _, id in ipairs(expand_pile) do
      room:setCardMark(Fk:getCardById(id), "tea_lianhe_visable-phase", 0)
    end
    if suc and #dat.cards == 1 then
      local cards = table.clone(dat.cards)
      if not table.contains(target:getCardIds("h"), cards[1]) and player ~= target then
        cards = table.random(target:getCardIds("h"), 1)
      end
      local tos = dat.targets
      local c = Fk:cloneCard("duel")
      c:addSubcards(cards)
      c.skillName = self.name
      local use = {
        from = player.id,
        tos = table.map(tos, function(id) return {id} end),
        card = c,
      }
      room:setPlayerMark(player, "@tea_lianhe_choose", {})
      room:setPlayerMark(player, "tea_lianhe_choose", {})
      local mark = player:getTableMark("tea_lianhe_record-turn")
      table.insertTableIfNeed(mark, tos)
      room:setPlayerMark(player, "tea_lianhe_record-turn", mark)
      room:useCard(use)
    end
  end,
}
local lianhe_delay = fk.CreateTriggerSkill{
  name = "#tea_lianhe_dalay",
  refresh_events = {fk.CardUseFinished, fk.Damaged},
  can_refresh = function(self, event, target, player, data)
    return table.contains((data.card or {}).skillNames or {}, lianhe.name) and player:hasSkill(self, true)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardUseFinished then
      if data.card:isVirtual() and #data.card.subcards == 1 then
        local cids = data.card.subcards
        local c = Fk:getCardById(cids[1])
        if c.trueName == "slash" and (room:getCardArea(c.id) == Card.DiscardPile or room:getCardArea(c.id) == Card.Processing) then
          room:obtainCard(player, c, true, fk.ReasonPrey)
        end
      end
    else
      if #player:getTableMark("tea_lianhe_choose") == 0 then
        room:setPlayerMark(player, "@tea_lianhe_choose", {target.general})
        room:setPlayerMark(player, "tea_lianhe_choose", {target.id})
      else
        local mark = player:getTableMark("tea_lianhe_choose")
        local shownmark = player:getTableMark("@tea_lianhe_choose")
        table.insertIfNeed(mark, target.id)
        table.insertIfNeed(shownmark, target.general)
        room:setPlayerMark(player, "tea_lianhe_choose", mark)
        room:setPlayerMark(player, "@tea_lianhe_choose", shownmark)
      end
    end
  end,
}
local lianhe_viewas = fk.CreateViewAsSkill{
  name = "tea_lianhe_viewas",
  mute = true,
  interaction = function()
    local all_names = {"duel"}
    local names = U.getViewAsCardNames(Self, "tea_lianhe_viewas", all_names)
    if #names > 0 then
      return U.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function (self, to_select, selected)
    if #selected == 0 then 
      if self.is_self then
        return table.contains(Self:getCardIds("h") or {}, to_select)
      else
        return table.contains(self.expand_pile or {}, to_select)
      end
    end
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard("duel")
    card.skillName = lianhe.name
    card:addSubcard(cards[1])
    return card
  end,
}
local lianhe_prohibit = fk.CreateProhibitSkill{
  name = "#tea_lianhe_prohibit",
  mute = true,
  frequency = Skill.Compulsory,
  is_prohibited = function(self, from, to, card)
    return from:hasSkill(self, true) and table.contains(from:getTableMark("tea_lianhe_record-turn"), to.id) and table.contains(card.skillNames, lianhe.name)
  end,
}
local lianhe_visable = fk.CreateVisibilitySkill{
  name = "#tea_lianhe_visable",
  card_visible = function(self, player, card)
    if player:hasSkill(lianhe, true) and card:getMark("tea_lianhe_visable-phase") > 0 then
      return false
    end
  end
}

local xifeng = fk.CreateTriggerSkill{
  name = "tea_xifeng",
  anim_type = "defensive",
  events = {fk.DamageCaused, fk.DamageInflicted},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.damage = 0
    local to = player
    if event == fk.DamageCaused then
      to = data.to
    end
    if #to:getCardIds("e") > 0 then
      local num = #to:getCardIds("e")
      room:obtainCard(to, to:getCardIds("e"), true, fk.ReasonPrey)
      if not to.dead then
        local cids = room:askForDiscard(to, num, num, true, self.name, false, ".", "#tea_xifeng-discard:::" .. tostring(num))
        cids = table.filter(cids, function(id)
          return Fk:getCardById(id).trueName == "slash"
        end)
        if #cids > 0 and not player.dead then
          room:askForYiji(player, cids, nil, self.name, nil, nil, "#tea_xifeng-yiji", cids)
        end
      end
    end
    return true
  end,
}
Fk:addSkill(lianhe_viewas)
lianhe:addRelatedSkill(lianhe_delay)
lianhe:addRelatedSkill(lianhe_prohibit)
lianhe:addRelatedSkill(lianhe_visable)
mohuba:addSkill(lianhe)
mohuba:addSkill(xifeng)
Fk:loadTranslationTable{
  ["tea__mohuba"] = "莫护跋",
  ["#tea__mohuba"] = "壆圩宗边",
  ["designer:tea__mohuba"] = "Joyeuse",
  ["cv:tea__mohuba"] = "茶社",
  ["illustrator:tea__mohuba"] = "林业通",
  ["tea_lianhe"] = "连壑",
  [":tea_lianhe"] = "出牌阶段，你可将体力值最大或手牌数最多的角色的一张手牌当【决斗】对一名本回合未以此法选中的角色使用。若转化底牌为"..
  "【杀】，此牌结算后你获得之。若因此受伤的角色存活，你下次发动此技能须选择其邻家的手牌。",
  ["tea_lianhe_viewas"] = "连壑",
  ["@tea_lianhe_choose"] = "连壑",
  ["#tea_lianhe-invoke"] = "你可以将一张牌当【决斗】使用",
  ["tea_xifeng"] = "熄锋",
  [":tea_xifeng"] = "每回合限一次，当你造成或受到伤害后，你可以防止此伤害，令受伤角色获得其装备区所有牌，并弃置 X 张牌（X 为其获得牌数）"..
  "，然后你可以将其以此法弃置的【杀】分配给任意名角色。",
  ["#tea_xifeng-discard"] = "熄锋：你须弃置 %arg 张牌",
  ["#tea_xifeng-yiji"] = "熄锋：你可以分配这些【杀】",
}

local chenqi = General(extension, "tea__chenqi", "shu", 3)
local chengquan = fk.CreateTriggerSkill{
  name = "tea_chengquan",
  anim_type = "support",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target.seat == 1 and target.phase == Player.Finish
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(target), function(p)
      return p:inMyAttackRange(target)
    end)
    if #targets > 0 then
      for _, p in ipairs(targets) do
        local use = room:askForUseCard(p, "slash", "slash", "#tea_chengquan-use:" .. target.id, true, {
          exclusive_targets = {target.id}, 
          bypass_distances = true,
        })
        if use then
          room:useCard(use)
        else
          p:drawCards(1, self.name)
          if not p:isKongcheng() then
            local cid = room:askForCard(p, 1, 1, false, self.name, false, ".", "#tea_chengquan-choose:" .. target.id)
            room:obtainCard(target, cid, false, fk.ReasonGive)
          end
        end
      end
    end
  end,
}
local roujia = fk.CreateTriggerSkill{
  name = "tea_roujia",
  anim_type = "support",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and not data.damageDealt and not player.room.current.dead and data.card.trueName == "slash"
  end,
  on_use = function(self, event, target, player, data)
    player.room.current:drawCards(1, self.name)
  end,
}
chenqi:addSkill(chengquan)
chenqi:addSkill(roujia)
Fk:loadTranslationTable{
  ["tea__chenqi"] = "陈祇",
  ["#tea__chenqi"] = "衿曆威容",
  ["designer:tea__chenqi"] = "暴食大王 Azusa",
  ["cv:tea__chenqi"] = "茶社",
  ["illustrator:tea__chenqi"] = "啪啪三国",
  ["tea_chengquan"] = "承权",
  [":tea_chengquan"] = "一号位的结束阶段，你可以令攻击范围内包含其的所有角色选择一项：1.摸一张牌并交给其一张手牌；2.对其使用一张【杀】。",
  ["#tea_chengquan-use"] = "承权：你须对 %src 使用一张【杀】，或点取消，摸一张牌并交给 %src 一张牌",
  ["#tea_chengquan-choose"] = "承权：交给 %src 一张牌",
  ["tea_roujia"] = "柔嘉",
  [":tea_roujia"] = "当一名角色使用【杀】结算结束后，若此【杀】未对目标角色造成伤害，你可以令当前回合角色摸一张牌。",
}

local jihuo = General(extension, "tea__jihuo", "shu", 4)
local suizuo = fk.CreateTriggerSkill{
  name = "tea_suizuo",
  anim_type = "support",
  events = {fk.RoundStart, fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    if event == fk.RoundStart then
      return player:hasSkill(self)
    else
      return player:hasSkill(self) and target.chained
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local targets = table.map(table.filter(room:getOtherPlayers(player), function(p)
        return not p.chained
      end), Util.IdMapper)
      local num = 1
      if player.chained and #targets > 1 then
        num = 2
      end
      local tos = room:askForChoosePlayers(player, targets, num, num, "#tea_suizuo-choose:::" .. tostring(num), self.name, true)
      if #tos > 0 then
        self.cost_data = tos
        return true
      end
    else
      return room:askForSkillInvoke(player, self.name, nil, "#tea_suizuo-invoke:" .. target.id) and player:getMark("tea_zuizuo_count-round") < player.hp
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.RoundStart then
      local targets = self.cost_data
      room:sortPlayersByAction(targets)
      if not player.chained then
        player:setChainState(true)
      end
      for _, id in ipairs(targets) do
        room:getPlayerById(id):setChainState(true)
      end
      for _, p in ipairs(room.alive_players) do
        if p.chained then
          p:drawCards(1, self.name)
        end
      end
    else
      room:addPlayerMark(player, "tea_zuizuo_count-round", 1)
      player:drawCards(1,self.name)
      if not player.dead and not player:isNude() and player ~= target then
        local cid = room:askForCard(player, 1, 1, true, self.name, false, ".", "#tea_suizuo-give:" .. target.id)
        if #cid == 1 then
          room:obtainCard(target, cid, false, fk.ReasonGive)
        end
      end
    end
  end,
}
local shanmeng = fk.CreateTriggerSkill{
  name = "tea_shanmeng",
  anim_type = "support",
  events = {fk.TargetConfirming},
  can_trigger = function(self, event, target, player, data)
    if player:usedSkillTimes(self.name, Player.HistoryTurn) == 0 and player:hasSkill(self) and target.chained then
      return table.find(player.room:getOtherPlayers(target), function(p)
        return p.chained
      end)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(target)
    for _, p in ipairs(targets) do
      if p.chained and not p:isNude() then
        local cid = room:askForCard(p, 1, 999, true, self.name, false, ".", "#tea_shanmeng-give:" .. target.id)
        if #cid > 0 then
          room:obtainCard(target, cid, false, fk.ReasonGive)
        end
      end
    end
  end,
}
jihuo:addSkill(suizuo)
jihuo:addSkill(shanmeng)
Fk:loadTranslationTable{
  ["tea__jihuo"] = "济火",
  ["#tea__jihuo"] = "罗甸王",
  ["designer:tea__jihuo"] = "终汐舷",
  ["cv:tea__jihuo"] = "茶社",
  ["illustrator:tea__jihuo"] = "食茸",
  ["tea_suizuo"] = "随佐",
  [":tea_suizuo"] = "每轮开始时，你可以横置你和一名其他角色（若你已横置，则改为横置两名其他角色），令所有已横置的角色各摸一张牌。每轮限 X 次（X"..
  " 为你的体力值），一名已横置的角色回合开始时，你可以摸一张牌并交给其一张牌。",
  ["#tea_suizuo-choose"] = "随佐：你可以横置 %arg 名角色",
  ["#tea_suizuo-invoke"] = "随佐：你可以摸一张牌并交给 %src 一张牌",
  ["#tea_suizuo-give"] = "你需要交给 %src 一张牌",
  ["tea_shanmeng"] = "山盟",
  [":tea_shanmeng"] = "每回合限一次，当一名已横置的角色成为牌的目标时，你可以令其他已横置的角色依次交给其任意张牌。",
  ["#tea_shanmeng-give"] = "山盟：你需要交给 %src 至少一张牌",
}
local dinggu = General(extension, "tea__dinggu", "wu", 2)
local mengsong = fk.CreateTriggerSkill{
  name = "tea_mengsong",
  anim_type = "special",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and ((player.phase == Player.Judge and #player:getCardIds("j") > 0) or player.phase == Player.Draw) then
      local card = Fk:cloneCard("bogus_flower")
      return U.canUseCardTo(player.room, player, player, card)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = Fk:cloneCard("bogus_flower")
    card.skillName = self.name
    if player.phase == Player.Judge then
      card:addSubcards(player:getCardIds("j"))
    else
      card:addSubcards(room:getNCards(1))
    end
    local use = {
      from = player.id,
      tos = {{player.id}},
      card = card,
    }
    room:useCard(use)
    return true
  end,
}
local xiangqian = fk.CreateActiveSkill{
  name = "tea_xiangqian",
  anim_type = "support",
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local cids = room:askForPoxi(player, "tea_xiangqian_poxi", {
      { target.general, target:getCardIds("h") },
      { player.general, player:getCardIds("h") },
    }, nil, true)
    if #cids > 0 then
      room:recastCard(cids, player, self.name)
    end
    if not player.dead and not target.dead then
      cids = room:askForPoxi(target, "tea_xiangqian_poxi", {
        { player.general, player:getCardIds("h") },
        { target.general, target:getCardIds("h") },
      }, nil, true)
      if #cids > 0 then
        room:recastCard(cids, target, self.name)
      end
    end
  end,
}
Fk:addPoxiMethod{
  name = "tea_xiangqian_poxi",
  card_filter = function(to_select, selected, data)
    if table.contains(data[2][2], to_select) then
      return table.find(data[1][2], function(id)
        return Fk:getCardById(id).trueName == Fk:getCardById(to_select).trueName
      end)
    end
  end,
  feasible = function(selected)
    return true
  end,
  prompt = "tea_xiangqian_poxi-ask",
}
local guye = fk.CreateTriggerSkill{
  name = "tea_guye",
  anim_type = "drawcard",
  events = {fk.EventPhaseEnd},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) then
      local room = player.room
      local list = {}
      room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.from and move.moveReason ~= fk.ReasonUse and move.moveReason ~= fk.ReasonResonpse then
            table.insert(list, {move.from, #move.moveInfo})
          end
        end
        return false
      end, Player.HistoryPhase)
      local targets = table.filter(room.alive_players, function(p)
        local counts = 0
        for _, v in ipairs(list) do
          if v[1] == p.id then
            counts = counts + v[2]
          end
        end
        return counts > 1
      end)
      if #targets > 0 then
        self.cost_data = targets
        return true
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = self.cost_data
    local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 999, "#tea_guye-invoke", self.name, true)
    if #tos > 0 then
      self.cost_data = tos
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = self.cost_data
    room:sortPlayersByAction(targets)
    for _, id in ipairs(targets) do
      room:getPlayerById(id):drawCards(1, self.name)
    end
  end,
}
dinggu:addSkill(mengsong)
dinggu:addSkill(xiangqian)
dinggu:addSkill(guye)
Fk:loadTranslationTable{
  ["tea__dinggu"] = "丁固",
  ["#tea__dinggu"] = "塞渊好德",
  ["designer:tea__dinggu"] = "孙笑川",
  ["cv:tea__dinggu"] = "茶社",
  ["illustrator:tea__dinggu"] = "啪啪三国",
  ["tea_mengsong"] = "梦松",
  [":tea_mengsong"] = "判定阶段/摸牌阶段，你可以改为将判定区里所有牌/牌堆顶的牌当做一张<a href='#dinggu_bogus_flower'>【树上开花】</a>使用。",
  ["#dinggu_bogus_flower"] = "<b>树上开花</b><br/>对你使用，你弃置至多两张牌，然后摸等量的牌；若弃置了装备牌，则多摸一张牌。",
  ["tea_xiangqian"] = "相谦",
  [":tea_xiangqian"] = "出牌阶段限一次，你可观看一名其他角色的手牌，若你手牌中有与其手牌中牌名相同的牌，你可重铸之。若如此做，"..
  "其观看你的手牌，若其手牌中有与你手牌中牌名相同的牌，其可重铸之。",
  ["tea_xiangqian_poxi"] = "相谦",
  ["tea_xiangqian_poxi-ask"] = "相谦：选择一些牌重铸",
  ["tea_guye"] = "固业",
  [":tea_guye"] = "每个阶段结束时，若有角色于此阶段不因使用或打出而失去至少两张牌，你可令其摸一张牌。",
  ["#tea_guye-invoke"] = "固业：你可以选择任意名角色，其摸一张牌",
}

local kangsenghui = General(extension, "tea__kangsenghui", "wu", 3)
local shanji = fk.CreateActiveSkill{
  name = "tea_shanji",
  anim_type = "support",
  target_filter = Util.FalseFunc,
  card_filter = Util.FalseFunc,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getCardIds("h") > 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    player:throwAllCards("h")
    for _, p in ipairs(room.alive_players) do
      if p:isWounded() and not p.dead then
        local choice = room:askForChoice(p, {"shanji_recover", "shanji_drawcards"}, self.name, "#tea_shanji_choose")
        if choice == "shanji_drawcards" then
          p:drawCards(2, self.name)
        else
          room:recover{
            who = p,
            num = 1,
            skillName = self.name,
          }
        end
      end
    end
  end,
}
local debao = fk.CreateTriggerSkill{
  name = "tea_debao",
  anim_type = "support",
  events = {fk.EnterDying},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player:isKongcheng() and player:getMark("@@tea_debao_mark-turn") == 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local suc = false
    for _, p in ipairs(room:getOtherPlayers(player)) do
      if not p:prohibitUse(Fk:cloneCard("peach")) and #p:getCardIds("h") > 1 and player:isWounded() then
        local cids = room:askForDiscard(p, 2, 2, false, self.name, true, ".", "#tea_debao-choose:" .. player.id, true)
        if #cids == 2 then
          room:throwCard(cids, self.name, p, p)
          if not p.dead then
            local card = Fk:cloneCard("peach")
            card.skillName = self.name
            local use = {
              from = p.id,
              tos = {{player.id}},
              card = card,
            }
            suc = true
            room:useCard(use)
          end
        end
      end
    end
    if suc then
      room:setPlayerMark(player, "@@tea_debao_mark-turn", 1)
    end
  end,
}
kangsenghui:addSkill(shanji)
kangsenghui:addSkill(debao)
Fk:loadTranslationTable{
  ["tea__kangsenghui"] = "康僧会",
  ["#tea__kangsenghui"] = "善济众生",
  ["designer:tea__kangsenghui"] = "夜伟",
  ["cv:tea__kangsenghui"] = "茶社",
  ["illustrator:tea__kangsenghui"] = "三剑豪",
  ["tea_shanji"] = "善济",
  [":tea_shanji"] = "出牌阶段限一次，你可以弃置所有手牌，令所有已受伤的角色依次选择一项：1.回复 1 点体力；2.摸两张牌。",
  ["#tea_shanji_choose"] = "善济：选择一项",
  ["shanji_recover"] = "回复1点体力",
  ["shanji_drawcards"] = "摸两张牌",
  ["tea_debao"] = "德报",
  [":tea_debao"] = "当你进入濒死状态时，若你没有手牌，你可以令其他角色依次选择是否弃置两张手牌并视为对你使用一张【桃】，"..
  "若有角色以此法对你使用【桃】，本回合此技能失效。",
  ["#tea_debao-choose"] = "德报：你可以弃置两张手牌，视为对 %src 使用一张【桃】",
  ["@@tea_debao_mark-turn"] = "德报失效",
}

local yuanwei = General(extension, "tea__yuanwei", "qun", 3)
local chizong = fk.CreateActiveSkill{
  name = "tea_chizong",
  anim_type = "support",
  interaction = function(self)
    local all_choices = {"chizong_recover", "chizong_drawcards"}
    local choices = {}
    if Self:getMark("tea_chizong_ma-turn") == 0 then
      table.insert(choices, "chizong_recover")
    end
    if Self:getMark("tea_chizong_mb-turn") == 0 then
      table.insert(choices, "chizong_drawcards")
    end
    return UI.ComboBox { choices = choices , all_choices = all_choices }
  end,
  target_num = 1,
  can_use = function(self, player)
    return player:getMark("tea_chizong_ma-turn") == 0 or player:getMark("tea_chizong_mb-turn") == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    if #selected == 0 and self.interaction.data then
      if self.interaction.data == "chizong_recover" then
        local tos = table.filter(Fk:currentRoom().alive_players, function(p)
          return table.every(Fk:currentRoom().alive_players, function(p1)
            return #p:getCardIds("h") >= #p1:getCardIds("h")
          end) and p:isWounded()
        end)
        tos = table.map(tos, Util.IdMapper)
        return table.contains(tos, to_select)
      else
        local tos = table.filter(Fk:currentRoom().alive_players, function(p)
          return table.every(Fk:currentRoom().alive_players, function(p1)
            return p.hp >= p1.hp
          end)
        end)
        tos = table.map(tos, Util.IdMapper)
        return table.contains(tos, to_select)
      end
    end
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local choice = self.interaction.data

    if choice == "chizong_recover" then
      room:setPlayerMark(player, "tea_chizong_ma-turn", 1)
      room:recover{
        who = target,
        num = 1,
        skillName = self.name,
      }
      room:setPlayerMark(target, "tea_chizong_a-turn", 1)
    else
      room:setPlayerMark(player, "tea_chizong_mb-turn", 1)
      target:drawCards(2, self.name)
      room:setPlayerMark(target, "tea_chizong_b-turn", 1)
    end

    if target:getMark("tea_chizong_a-turn") > 0 and target:getMark("tea_chizong_b-turn") > 0 and not target.dead then
      room:delay(500)
      local c = Fk:cloneCard("archery_attack")
      c.skillName = self.name
      local use = {
        from = target.id,
        card = c,
      }
      if not player:prohibitUse(c) then
        room:useCard(use)
      end
    end
  end,
}
local yihuo = fk.CreateTriggerSkill{
  name = "tea_yihuo",
  anim_type = "masochism",
  events = {fk.Damaged},
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    for _ = 1, data.damage do
      if self.cancel_cost or not player:hasSkill(self) then break end
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local choice = room:askForChoice(player, {"1", "2", "3", "Cancel"}, self.name, "#tea_yihuo-invoke")
    if choice ~= "Cancel" then
      self.cost_data = tonumber(choice)
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local num = self.cost_data
    player:drawCards(num, self.name)
    local tos = table.filter(room.alive_players, function(p)
      return table.every(room.alive_players, function(p1)
        return #p:getCardIds("h") >= #p1:getCardIds("h")
      end)
    end)
    for _, p in ipairs(tos) do
      if not p.dead then
        room:askForDiscard(p, #tos, #tos, false, self.name, false, ".", "#tea_yihuo-discard:::" .. tostring(#tos))
      end
    end
  end,
}
yuanwei:addSkill(chizong)
yuanwei:addSkill(yihuo)
Fk:loadTranslationTable{
  ["tea__yuanwei"] = "袁隗",
  ["#tea__yuanwei"] = "以身事狼",
  ["designer:tea__yuanwei"] = "暴食大王 Azusa",
  ["cv:tea__yuanwei"] = "茶社",
  ["illustrator:tea__yuanwei"] = "武将列传",
  ["tea_chizong"] = "持宗",
  [":tea_chizong"] = "出牌阶段各限一次，你可以令一名手牌数最多/体力值最大的角色回复 1 点体力/摸两张牌。当你每回合对同一名角色"..
  "发动两次“持宗”后，其视为使用一张【万箭齐发】。",
  ["chizong_recover"] = "回复1点体力",
  ["chizong_drawcards"] = "摸两张牌",
  ["tea_yihuo"] = "溢祸",
  [":tea_yihuo"] = "当你受到 1 点伤害后，你可以摸一至三张牌，然后令手牌数最多的角色依次弃置 X 张手牌（X 为需要弃牌的角色数）。",
  ["#tea_yihuo-invoke"] = "溢祸：选择摸牌数",
  ["#tea_yihuo-discard"] = "溢祸：你须弃置 %arg 张手牌",
}

local beigongboyu = General(extension, "tea__beigongboyu", "qun", 4)
local woluan = fk.CreateTriggerSkill{
  name = "tea_woluan",
  anim_type = "special",
  frequency = Skill.Compulsory,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local all_choices = {"woluan_drawcards", "woluan_discard", "woluan_addition", "woluan_attackrange"}
    local choices = room:askForChoices(player, all_choices, 1, 4, self.name, "#tea_woluan-choice", false)
    local num = 4 - #choices
    if num > 0 then
      if table.contains(choices, "woluan_drawcards") then
        player:drawCards(num, self.name)
      end
      if table.contains(choices, "woluan_discard") then
        local targets = table.filter(room.alive_players, function (p) return not p:isNude() end)
        if #targets >= num then 
          local tos = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, num, "#tea_wuluan-choose", self.name, true)
          if #tos > 0 then
            room:sortPlayersByAction(tos)
            for _, pid in ipairs(tos) do
              if player.dead then break end
              local to = room:getPlayerById(pid)
              if not to:isNude() then
                local id = room:askForCardChosen(player, to, "he", self.name)
                room:throwCard({id}, self.name, to, player)
              end
            end
          end
        end
      end
      if table.contains(choices, "woluan_addition") then
        room:setPlayerMark(player, "tea_wuluan_addition-turn", num - 1)
      end
      if table.contains(choices, "woluan_attackrange") then
        room:setPlayerMark(player, "tea_wuluan_attackrange-turn", num)
      end
    end
  end,

  refresh_events = {fk.CardUsing},
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(self) and player:getMark("tea_wuluan_addition-turn") > 0 then
      local logic = player.room.logic
      local suc = {}
      logic:getEventsOfScope(GameEvent.UseCard, 999, function (e)
        local use = e.data[1]
        if use.from == player.id then
          table.insertIfNeed(suc, use)
        end
        return false
      end, Player.HistoryTurn)
      return #suc == 1
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    data.additionalEffect = (data.additionalEffect or 0) + player:getMark("tea_wuluan_addition-turn")
    room:setPlayerMark(player, "tea_wuluan_addition-turn", 0)
  end,
}
local woluan_attackrange = fk.CreateAttackRangeSkill{
  name = "#tea_woluan_attackrange",
  correct_func = function (self, from, to)
    return from:getMark("tea_wuluan_attackrange-turn")
  end,
}
local raoguo = fk.CreateTargetModSkill{
  name = "tea_raoguo",
  frequency = Skill.Compulsory,
  bypass_times = function(self, player, skill, scope, card, to)
    return player:hasSkill(self) and (skill.trueName == "slash_skill" or card:isCommonTrick())
  end,
  bypass_distances =  function(self, player, skill, card, to)
    return player:hasSkill(self) and (skill.trueName == "slash_skill"or card:isCommonTrick())
  end,
}
local raoguo_prohibit = fk.CreateProhibitSkill{
  name = "#tea_raoguo_prohibit",
  is_prohibited = function(self, from, to, card)
    if (card.trueName == "slash" or card:isCommonTrick()) and from:hasSkill(raoguo, true) then
      local fathest = 0
      local targets = {}
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if from ~= p and from:inMyAttackRange(p) then
          local distance = from:distanceTo(p)
          if distance > fathest then
            fathest = distance
            targets = { p.id }
          elseif distance == fathest then
            table.insert(targets, p.id)
          end
        end
      end
      return not table.contains(targets, to.id)
    end
  end,
}
woluan:addRelatedSkill(woluan_attackrange)
raoguo:addRelatedSkill(raoguo_prohibit)
beigongboyu:addSkill(woluan)
beigongboyu:addSkill(raoguo)
Fk:loadTranslationTable{
  ["tea__beigongboyu"] = "北宫伯玉",
  ["#tea__beigongboyu"] = "群蚍撼柱",
  ["designer:tea__beigongboyu"] = "小叶子",
  ["cv:tea__beigongboyu"] = "茶社",
  ["illustrator:tea__beigongboyu"] = "武将列传 ",
  ["tea_woluan"] = "握乱",
  [":tea_woluan"] = "锁定技，准备阶段，你选择任意项：1.你摸 X 张牌；2.你弃置X名角色各一张牌；3.本回合你使用的第一张牌执"..
  "行X次（至少为1）；4.本回合你的攻击范围+X（X 为你未选择的项数）。",
  ["#tea_woluan-choice"] = "握乱：选择任意项",
  ["woluan_drawcards"] = "摸X张牌",
  ["woluan_discard"] = "弃置X名角色各一张牌",
  ["woluan_addition"] = "本回合首张牌结算X次",
  ["woluan_attackrange"] = "本回合攻击距离+X",
  ["tea_raoguo"] = "扰国",
  [":tea_raoguo"] = "锁定技，你使用【杀】和普通锦囊牌无距离和次数限制且仅能指定你攻击范围内与你距离最远的角色为目标。",
  ["#tea_raoguo_prohibit"] = "扰国",
}

local hezeng = General(extension, "tea__hezeng", "jin", 3)
local shiwan = fk.CreateTriggerSkill{
  name = "tea_shiwan",
  anim_type = "special",
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and player == target and player.phase == Player.Start
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    isFirst = true
    while isFirst or room:askForSkillInvoke(player, self.name, nil, "#tea_shiwan-invoke") do
      player:drawCards(1, self.name)
      if isFirst then
        isFirst = false
      end
      local num = 0
      room.logic:getEventsOfScope(GameEvent.MoveCards, 999, function (e)
        for _, move in ipairs(e.data) do
          if move.moveReason == fk.ReasonDraw and move.to == player.id and move.toArea == Player.Hand then
            num = num + #move.moveInfo
          end
        end
        return false
      end, Player.HistoryPhase)
      local targets = table.filter(room.alive_players, function(p)
        return table.every(room:getOtherPlayers(p), function(p1)
          return #p:getCardIds("h") >= #p1:getCardIds("h")
        end)
      end)
      if #targets > 0 then
        for _, p in ipairs(targets) do
          room:askForDiscard(p, num, 999, true, self.name, false, ".", "#tea_shiwan-discard:::" .. tostring(num))
        end
        if table.contains(targets, player) then break end
      end
    end
  end,
}
local xiluan = fk.CreateTriggerSkill{
  name = "tea_xiluan",
  anim_type = "special",
  frequency = Skill.Limited,
  events = {fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(self) and player == target and player.phase == Player.Finish and player:usedSkillTimes(self.name, Player.HistoryGame) == 0 then
      local list = {}
      player.room.logic:getEventsOfScope(GameEvent.MoveCards, 1, function (e)
        for _, move in ipairs(e.data) do
          if move.toArea == Card.DiscardPile then
            for _, info in ipairs(move.moveInfo) do
              if player.room:getCardArea(Fk:getCardById(info.cardId)) == Card.DiscardPile and Fk:getCardById(info.cardId).is_damage_card then
                table.insertIfNeed(list, info.cardId)
              end
            end
          end
        end
        return false
      end, Player.HistoryTurn)
      if #list > 0 then
        self.cost_data = list
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = self.cost_data
    local suc, dat = room:askForUseActiveSkill(player, "tea_xiluan_choose_skill", "#tea_xiluan-invoke", false, {
      expand_pile = cards,
    })
    if suc then
      if #dat.targets == 1 and #dat.cards > 0 then
        local to = room:getPlayerById(dat.targets[1])
        to:addToPile("#tea_xiluan_pile", dat.cards, true, self.name, nil, table.map(room.alive_players, Util.IdMapper))
        room:setPlayerMark(to, "@$tea_xiluan_mark", table.map(dat.cards, function(id)
          return Fk:getCardById(id).name
        end))
      end
    end
  end,
}
local xiluan_delay = fk.CreateTriggerSkill{
  name = "#tea_xiluan_delay",
  refresh_events = {fk.DamageInflicted},
  can_refresh = function(self, event, target, player, data)
    if player == target and #player:getPile("#tea_xiluan_pile") > 0 then
      return table.find(player:getPile("#tea_xiluan_pile"), function(id)
        return Fk:getCardById(id).trueName == data.card.trueName
      end)
    end
  end,
  on_refresh = function(self, event, target, player, data)
    if player.room:getTag("RoundCount") < 3 then
      player.room:notifySkillInvoked(player, xiluan.name, "defensive")
      data.damage = data.damage - 1
    else
      player.room:notifySkillInvoked(player, xiluan.name, "negative")
      data.damage = data.damage + 1
    end
  end,
}
local xiluan_choose_skill = fk.CreateActiveSkill{
  name = "tea_xiluan_choose_skill",
  min_card_num = 1,
  target_num = 1,
  card_filter = function (self, to_select, selected)
    return table.contains(self.expand_pile, to_select)
  end,
  target_filter = function (self, to_select, selected)
    return #selected == 0
  end,
}
Fk:addSkill(xiluan_choose_skill)
xiluan:addRelatedSkill(xiluan_delay)
hezeng:addSkill(shiwan)
hezeng:addSkill(xiluan)
Fk:loadTranslationTable{
  ["tea__hezeng"] = "何曾",
  ["#tea__hezeng"] = "见宴知乱",
  ["designer:tea__hezeng"] = "奥料",
  ["cv:tea__hezeng"] = "茶社",
  ["illustrator:tea__hezeng"] = "率土之滨",
  ["tea_shiwan"] = "食万",
  [":tea_shiwan"] = "准备阶段，你可以摸一张牌，然后令手牌数最多的角色依次弃至少 X 张牌，若这些角色不包含你，你可以重复此流程（X 为你此阶段的摸牌数）。",
  ["#tea_shiwan-invoke"] = "食万：你可以重复此流程",
  ["#tea_shiwan-discard"] = "食万：你须弃置至少 %arg 张牌",
  ["tea_xiluan"] = "悉乱",
  [":tea_xiluan"] = "限定技，结束阶段，你可以将本回合进入弃牌堆的任意张伤害牌明置于一名角色的武将牌旁，若当前游戏轮数小于 3/不小于 3，"..
  "其受到同名牌造成的伤害-1/+1。",
  ["#tea_xiluan-invoke"] = "悉乱：将任意张伤害牌置于一名角色武将牌旁",
  ["tea_xiluan_choose_skill"] = "悉乱",
  ["#tea_xiluan_pile"] = "悉乱",
  ["@$tea_xiluan_mark"] = "悉乱",
}

local tuobashamohan = General(extension, "tea__tuobashamohan", "jin", 4)
local tongzhi = fk.CreateActiveSkill{
  name = "tea_tongzhi",
  anim_type = "control",
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and #(Fk:currentRoom():getPlayerById(to_select):getCardIds("h")) > 0
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name, Player.HistoryPhase) == 0 and #player:getCardIds("h") > 0
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local targets = {player, target}
    local extraData = {
      num = 2,
      min_num = 2,
      include_equip = false,
      pattern = ".",
      reason = self.name,
    }
    local prompt = "#askForTongzhi"
    local data = { "choose_cards_skill", prompt, false, extraData }
    local req = Request:new(targets, "AskForUseActiveSkill")
    req.focus_text = "#tea_tongzhi_considering"
    local req_table = {}
    for _, to in ipairs(targets) do
      if #to:getCardIds("h") > 1 then
        table.insert(req_table, to.id)
        req:setData(to, data)
      end
    end

    for _, p in ipairs(targets) do
      local cids = {}
      if table.contains(req_table, p.id) then
        if p.reply_ready then
          local replyCard = req:getResult(p).card
          cids = table.random(replyCard.subcards, 2)
        elseif #p:getCardIds("h") > 1 then
          cids = table.random(p:getCardIds(Player.Hand), 2)
        else
          cids = p:getCardIds("h")
        end
      else
        if #p:getCardIds("h") > 1 then
          cids = table.random(p:getCardIds(Player.Hand), 2)
        else
          cids = p:getCardIds("h")
        end
      end
      if cids then
        if p == player then
          target:addToPile("$tea_tongzhi", cids, false, self.name, nil, {target.id, player.id})
        else
          player:addToPile("$tea_tongzhi", cids, false, self.name, nil, {target.id, player.id})
        end
      end
    end
  end,
}
local tongzhi_delay = fk.CreateTriggerSkill{
  name = "#tea_tongzhi_delay",
  refresh_events = {fk.TurnStart},
  can_refresh = function(self, event, target, player, data)
    return target == player and #player:getPile("$tea_tongzhi") > 0
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    local cids = player:getPile("$tea_tongzhi")
    room:obtainCard(player, cids, false, fk.ReasonJustMove)
  end,
}
local isBetween = function(num1, num2, num)
  return (num1 < num and num2 > num) or (num1 > num and num2 < num)
end
local yishu = fk.CreateViewAsSkill{
  name = "tea_yishu",
  anim_type = "special",
  pattern = "slash",
  prompt = "#tea_yishu-invoke",
  interaction = function()
    local all_names = {"tea_shot__slash"}
    local names = U.getViewAsCardNames(Self, "tea_yishu", all_names)
    if #names > 0 then
      return U.CardNameBox { choices = names, all_choices = all_names }
    end
  end,
  card_filter = function(self, to_select, selected)
    if table.contains(Self:getCardIds("h"), to_select) then
      if #selected == 0 then return true
      elseif #selected == 1 then
        return table.find(Self:getCardIds("h"), function(id)
          return isBetween(Fk:getCardById(selected[1]).number, Fk:getCardById(to_select).number, Fk:getCardById(id).number)
        end)
      end
    end
  end,
  view_as = function(self, cards)
    if not self.interaction.data or #cards ~= 2 then return end
    local card = Fk:cloneCard(self.interaction.data)
    self.cost_data = cards
    card.skillName = self.name
    return card
  end,
  enabled_at_play = function(self, player)
    return #player:getCardIds("h") > 1
  end,
  enabled_at_response = function(self, player, response)
    return not response and #player:getCardIds("h") > 1
  end,
  before_use = function(self, player, use)
    local cards = self.cost_data
    local room = player.room
    if #cards == 2 then
      local num1 = Fk:getCardById(cards[1]).number
      local num2 = Fk:getCardById(cards[2]).number
      local ids = table.filter(player:getCardIds("h"), function(id)
        local num = Fk:getCardById(id).number
        return isBetween(num1, num2, num)
      end)
      if #ids > 0 then
        local cid = room:askForCard(player, 1, 1, true, self.name, true, tostring(Exppattern{ id = ids }), "#tea_yishu-choose")
        if #cid == 1 then
          player:showCards(cards)
          if #player:getPile("$tea_tongzhi") == 2 then
            local num1 = Fk:getCardById(player:getPile("$tea_tongzhi")[1]).number
            local num2 = Fk:getCardById(player:getPile("$tea_tongzhi")[2]).number
            if isBetween(num1, num2, Fk:getCardById(cid[1]).number) then
              local tos = table.map(use.tos, function(t)
                return t[1]
              end)
              room:damage{
                to = player,
                damage = 1,
                skillName = self.name,
              }
              room:sortPlayersByAction(tos)
              for _, id in ipairs(tos) do
                local to = room:getPlayerById(id)
                room:damage{
                  to = to,
                  damage = 1,
                  skillName = self.name,
                }
              end
              tos = table.filter(tos, function(id)
                return not room:getPlayerById(id).dead
              end)
              if #tos == 0 then 
                return self.name 
              else
                use.tos = table.map(tos, function(id)
                  return {id}
                end)
              end
            end
          end
          use.card:addSubcards(cid)
        else
          return self.name
        end
      else
        return self.name
      end
    else
      return self.name
    end
  end,
}
tongzhi:addRelatedSkill(tongzhi_delay)
tuobashamohan:addSkill(tongzhi)
tuobashamohan:addSkill(yishu)
Fk:loadTranslationTable{
  ["tea__tuobashamohan"] = "拓跋沙漠汗",
  ["#tea__tuobashamohan"] = "异邻术裁毙",
  ["designer:tea__tuobashamohan"] = "cyc",
  ["cv:tea__tuobashamohan"] = "茶社",
  ["illustrator:tea__tuobashamohan"] = "啪啪三国",
  ["tea_tongzhi"] = "通质",
  [":tea_tongzhi"] = "出牌阶段限一次，你可以选择一名其他角色，你与其将两张手牌扣置于对方的武将牌上（不足则全扣），然后于各自"..
  "的回合开始时获得各自武将牌上的牌。",
  ["#askForTongzhi"] = "通质：选择两张牌",
  ["#tea_tongzhi_considering"] = "通质",
  ["$tea_tongzhi"] = "通质",
  ["tea_yishu"] = "疑术",
  [":tea_yishu"] = "当你需要使用一张射【杀】时，你可以展示两张手牌，并将一张点数位于这两张牌之间的牌当射【杀】使用。若此牌点"..
  "数处于你武将牌上“通质”牌点数之间，你与此牌目标各受到1点无来源伤害。",
  ["#tea_yishu-choose"] = "选择一张牌转化此射【杀】",
  ["#tea_yishu-invoke"] = "疑术：选择两张手牌展示",
}

return extension